import pandas as pd
import json
import plotly.express as px
from pathlib import Path

# 将数据作为字符串读取并转换为Python对象
path = Path("eq_data/eq_data_1_day_m1.geojson")
try:
    contents = path.read_text()
except:
    contents = path.read_text(encoding='utf-8')
# 将contents中的json数据解析为Python对象
all_eq_data = json.loads(contents)

# 将数据文件转换为更易阅读的版本
path = Path('readable_eq_data_7days.geojson')
# 将all_eq_data转换为JSON格式，并设置缩进为4个空格
readable_contents = json.dumps(all_eq_data,indent=4)
path.write_text(readable_contents)

all_eq_dicts = all_eq_data['features']

# 提取震级，位置等数据
lons,lats,titles,mags = [],[],[],[]
all_title = all_eq_data["metadata"]["title"]
for eq_dict in all_eq_dicts:
    lons.append(eq_dict['geometry']['coordinates'][0])
    lats.append(eq_dict['geometry']['coordinates'][1])
    titles.append(eq_dict["properties"]["title"])
    mags.append(eq_dict["properties"]["mag"])

# 封装数据
data = pd.DataFrame(
    data = zip(lons,lats,titles,mags),columns=['经度','纬度','位置','震级']
)

# 绘制地震散点图
fig = px.scatter(
    data,
    x='经度',
    y='纬度',
    range_x=[-200,200],
    range_y=[-90,90],
    width=800,
    height=800,
    title=all_title,
    size='震级',
    size_max=10,
    color='震级',
    hover_data='位置',
)

fig.write_html('eq_explore_data_7days.html')
fig.show()